package com.one.group.admin.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.ContractMapper;

import com.one.group.model.dto.ChargeStandardsFindDto;
import com.one.group.model.dto.ContractInsertDto;
import com.one.group.model.dto.ContractOneUpdateDto;
import com.one.group.model.dto.ContractQueryDto;
import com.one.group.model.entity.Contract;
import com.one.group.model.entity.ParkingSpace;
import com.one.group.model.entity.Resource;
import com.one.group.model.vo.ParkingContractListVo;
import com.one.group.model.vo.ParkingContractVo;
import com.one.group.service.ContractService;

import com.one.group.service.ParkingSpaceService;
import com.one.group.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {
    @Autowired
    ResourceService resourceService;
    @Autowired
    ParkingSpaceService parkingSpaceService;
    public List<String> findChargeStandardsByDto(ChargeStandardsFindDto chargeStandardsFindDto) {
        return this.getMapper().findChargeStandardsByDto(chargeStandardsFindDto);
    }

    @Override
    public List<ParkingContractListVo> findContractByQueryDto(ContractQueryDto contractQueryDto) {
        return this.getMapper().findContractByQueryDto(contractQueryDto);
    }

    @Override
    public Boolean InsertContractByDto(ContractInsertDto contractInsertDto) {
        Resource resource = new Resource();
        BeanUtils.copyProperties(contractInsertDto,resource);
        resourceService.getMapper().insertSelective(resource);
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractInsertDto,contract);
        contract.setResourceId(resource.getId());
        this.getMapper().insertSelective(contract);
        return true;
    }

    @Override
    @Transactional
    public Boolean updateContractByDto(ContractInsertDto contractInsertDto) {
        log.info("Entering updateContractByDto with ID: {}", contractInsertDto.getId());

        Contract contract1 = this.getMapper().selectOneById(contractInsertDto.getId());
        log.info("Found contract with resource ID: {}", contract1.getResourceId());

        // 更新资源
        Resource resource = new Resource();
        resource.setCarNumber(contractInsertDto.getCarNumber());
        resource.setParkingSpaceId(contractInsertDto.getParkingSpaceId());
        resource.setId(contract1.getResourceId());
        log.info("Attempting to update resource with ID: {}", resource.getId());
        int rows = resourceService.getMapper().update(resource);
        log.info("Resource update affected {} rows", rows);

        // 更新合同
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractInsertDto, contract);
        contract.setId(contractInsertDto.getId());
        log.info("Attempting to update contract with ID: {}", contract.getId());
        rows = this.getMapper().update(contract);
        log.info("Contract update affected {} rows", rows);

        return true;
    }
    @Override
    public Boolean DeleteContractById(Long id) {
        Contract contract = this.getMapper().selectOneById(id);
        int i = resourceService.getMapper().deleteById(contract.getResourceId());
        this.getMapper().deleteById(id);
        return true;
    }

    @Override
    public ParkingContractVo findContractById(Long id) {
        Contract contract = this.getMapper().selectOneById(id);
        Resource resource = resourceService.getMapper().selectOneById(contract.getResourceId());
        ParkingContractVo parkingContractVo = new ParkingContractVo();
        BeanUtils.copyProperties(contract,parkingContractVo);
        BeanUtils.copyProperties(resource,parkingContractVo);
        return parkingContractVo;
    }

    @Override
    public ParkingSpace getParkingSpaceByCellId(Long CellId) {
        return null;
    }

    @Override
    public Boolean InsertOneContractByDto(ContractOneUpdateDto contractOneUpdateDto) {
        return null;
    }


}
